<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="请输入单据编号"
      ></el-input>
      <el-select
        v-model="search.type"
        style="margin-left: 20px"
        placeholder="请选择出入库状态类型"
      >
        <el-option
          v-for="item in typeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        v-model="search.is_has_stock"
        style="margin-left: 20px"
        placeholder="请选择是否有库存"
      >
        <el-option
          v-for="item in is_has_stockList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        v-model="search.hangcai"
        style="margin-left: 20px"
        placeholder="请选择是否行采"
      >
        <el-option
          v-for="item in hangcaiList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        v-model="search.is_has_pd"
        style="margin-left: 20px"
        placeholder="请选择是否有生产日期"
      >
        <el-option
          v-for="item in is_has_pdList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.days"
        placeholder="请输入超生产日期天数"
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="dates"
        type="daterange"
        :clearable="false"
        range-separator="至"
        f
        start-placeholder=" 生产日期开始时间"
        end-placeholder=" 生产日期结束时间"
      ></el-date-picker>
      <el-select
        v-model="kinds"
        multiple
        collapse-tags
        style="margin-left: 20px"
        placeholder="请选择出入库类型"
      >
        <el-option
          v-for="item in businessType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend"
          >类别</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend"
          >供应商</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend"
          >仓库</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>
      <el-select
        style="margin-bottom: 5px"
        v-model="search.organization"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择机构"
        :remote-method="organizationMethod"
        :loading="insides.loading"
      >
        <el-option
          v-for="item in insides.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derives()"
        >导出xlsx</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column prop="id" label="ID" align="center"></el-table-column>
      <el-table-column
        prop="code"
        label="单据编号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="hangcai_desc"
        label="行采状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="spec"
        label="规格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dccode"
        label="配送中心编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dcname"
        label="配送中心名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="whsecode"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="whsename"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_code"
        label="分类编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_name"
        label="分类名称"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="num"
        label="出入库数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_num"
        label="剩余数量"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="price"
        label="出入库价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="出入库总价"
        align="center"
      ></el-table-column>
      <el-table-column prop="qty" label="箱数" align="center"></el-table-column>
      <el-table-column
        prop="stock_qty"
        label="剩余箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rk_spec"
        label="入库箱规"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label="制单时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="org_name"
        label="店铺名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="produce_date"
        label="生产日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="days"
        label=" 超生产日期天数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="shelf_life"
        label="保质期（天）"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="due_date"
        label="到期日"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
    <supplierSearch
      :supplierdialog="supplierdialog"
      @tag="supplierTag"
    ></supplierSearch>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import CommodityDialog from "@/components/superGrossMargin/commodityDialog";
import { information } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";
import ClassDialog from "@/components/superGrossMargin/classDialog";
import SupplierSearch from "@/components/superGrossMargin/supplierSearch";
import { getOrgData } from "@/api/middleorder/middleorderlist";
import Warehouseselect from "@/components/commodity/warehouseselect";
export default {
  components: {
    pagination: Pagination,
    supplierSearch: SupplierSearch,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog,
    warehouseselect: Warehouseselect
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        code: "",
        kind: "",
        start_time: "",
        end_time: "",
        organization: "",
        type: "",
        is_has_stock: "",
        is_has_pd: "",
        days: "",
        produce_start_time: "",
        produce_end_time: "",
        hangcai: ""
      },
      dates: [],
      hangcaiList: [
        {
          value: -1,
          label: "全部"
        },
        {
          value: 0,
          label: "非行采"
        },
        {
          value: 1,
          label: "行采"
        }
      ],
      is_has_pdList: [
        {
          value: 0,
          label: "全部"
        },
        {
          value: 1,
          label: "有生产日期"
        },
        {
          value: 2,
          label: "无生产日期"
        }
      ],
      is_has_stockList: [
        {
          value: 0,
          label: "全部"
        },
        {
          value: 1,
          label: "有库存"
        },
        {
          value: 2,
          label: "无库存"
        },
        {
          value: 10,
          label: "有库存(无时间限制)"
        }
      ],
      typeList: [
        {
          value: 1,
          label: "入库"
        },
        {
          value: 2,
          label: "出库"
        }
       
      ],
      kinds: [],
      insides: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      businessType: [
        {
          value: "10",
          label: "购货入库"
        },
        {
          value: "11",
          label: "商超直购供应商入库"
        },
        {
          value: "12",
          label: "高退低进入库"
        },

        {
          value: "16",
          label: "盘点入库"
        },

        {
          value: "20",
          label: "订单发货出库"
        },
        {
          value: "21",
          label: "订单中的供应商商品直接出库"
        },
        {
          value: "22",
          label: "高退低进出库"
        },
        {
          value: "23",
          label: "返货出库"
        },

        {
          value: "25",
          label: "商超ERP订单出库 "
        },

        {
          value: "28",
          label: "盘点出库"
        },
        {
          value: "29",
          label: "强配订单出库"
        },
        {
          value: "40",
          label: "ERP门店退货入库"
        },
        {
          value: "50",
          label: "加工出库"
        },
        {
          value: "90",
          label: "调拨出库"
        },
        {
          value: "91",
          label: "调拨入库"
        },
        {
          value: "102",
          label: "同步库存入库"
        }
      ],
      date: [],
      warehouseselect: {
        centerDialogVisible: false
      },
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      classdialog: {
        centerDialogVisible: false
      },
      classNameList: {
        name: "",
        code: ""
      },
      supplierdialog: {
        centerDialogVisible: false
      },
      supplierNameList: {
        name: "",
        code: ""
      },
      warehouseList: {
        name: "",
        code: ""
      }
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    this.getData();
    this.getOrg();
  },
  methods: {
    //获取页面数据
    getData() {
      console.log(121212);
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        kindstr: this.kinds.join(","),
        code: this.search.code,
        type: this.search.type,
        erp_org_code: this.search.organization,
        category_code: this.classNameList.code,
        supplier_code: this.supplierNameList.code,
        whsecode: this.warehouseList.code,
        goods_code: this.commodityNameList.code,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
        is_has_stock: this.search.is_has_stock,
        is_has_pd: this.search.is_has_pd,
        days: this.search.days,
        produce_start_time: this.search.produce_start_time,
        produce_end_time: this.search.produce_end_time,
        hangcai: this.search.hangcai == -1 ? "" : this.search.hangcai
      };
      information(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.paginate.data;
            this.amountTo = res.data.amountTo;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getOrg(key = "") {
      let data = {
        page: 1,
        limit: 500,
        key
      };
      //获取机构
      getOrgData(data).then(res => {
        if (res.data) {
          this.insides.option = res.data.map(item => {
            return { value: `${item.erp_org_code}`, label: `${item.name}` };
          });
        }
      });
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true
      };
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //获取供应商数据
    organizationMethod(query) {
      if (query !== "") {
        this.getOrg(query);
      } else {
        this.insides.option = [];
      }
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });

      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.code = arrCode.join(",");
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true
      };
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (index != 12 && index != 15 && index != 16) {
            sums[index] = "";
          } else {
            sums[12] = this.amountTo.num;
            sums[15] = this.amountTo.amount;
            sums[16] = this.amountTo.qty;
          }
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      if (this.dates.length > 0) {
        this.search.produce_start_time = this.dates[0];
        this.search.produce_end_time = this.dates[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true
      };
    },
    //导出
    derive() {
      var kindvalue = this.kinds.join(",");
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let hangcai =
            this.search.hangcai == -1 ? "" : this.search.hangcai;
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/goods/exportInformation?token=" +
            getToken() +
            "&code=" +
            this.search.code +
            "&type=" +
            this.search.type +
            "&category_code=" +
            this.classNameList.code +
            "&supplier_code=" +
            this.supplierNameList.code +
            "&erp_org_code=" +
            this.search.organization +
            "&whsecode=" +
            this.warehouseList.code +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&kindstr=" +
            kindvalue +
            "&is_has_stock=" +
            this.search.is_has_stock +
            "&is_has_pd=" +
            this.search.is_has_pd +
            "&days=" +
            this.search.days +
            "&produce_start_time=" +
            this.search.produce_start_time +
            "&produce_end_time=" +
            this.search.produce_end_time +
            "&hangcai=" +
            hangcai +
            "&goods_code=" +
            this.commodityNameList.code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //导出xlsx
    derives() {
      var kindvalue = this.kinds.join(",");
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let hangcai =
            this.search.hangcai == -1 ? "" : this.search.hangcai;
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/goods/exportInformationXlsx?token=" +
            getToken() +
            "&code=" +
            this.search.code +
            "&type=" +
            this.search.type +
            "&category_code=" +
            this.classNameList.code +
            "&supplier_code=" +
            this.supplierNameList.code +
            "&erp_org_code=" +
            this.search.organization +
            "&whsecode=" +
            this.warehouseList.code +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&kindstr=" +
            kindvalue +
            "&is_has_stock=" +
            this.search.is_has_stock +
            "&is_has_pd=" +
            this.search.is_has_pd +
            "&days=" +
            this.search.days +
            "&produce_start_time=" +
            this.search.produce_start_time +
            "&produce_end_time=" +
            this.search.produce_end_time +
            "&hangcai=" +
            hangcai +
            "&goods_code=" +
            this.commodityNameList.code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //清空
    empty() {
      this.dates = [];
      this.date = [];
      this.kinds = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        code: "",
        kind: "",
        start_time: "",
        end_time: "",
        organization: "",
        type: "",
        is_has_stock: "",
        is_has_pd: "",
        days: "",
        produce_start_time: "",
        produce_end_time: "",
        hangcai: ""
      };
      this.commodityNameList.name = "";
      this.commodityNameList.code = "";
      this.classNameList.name = "";
      this.classNameList.code = "";
      this.supplierNameList.name = "";
      this.supplierNameList.code = "";
      this.warehouseList.name = "";
      this.warehouseList.code = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}

.pagination {
  text-align: right;
  padding: 10px;
}
</style>
